﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Collections.ObjectModel
Imports System.Linq
Imports Newtonsoft.Json
Imports roncliProductions.LibWowArmory.Enums
Imports roncliProductions.LibWowArmory.Extensions
Imports roncliProductions.LibWowArmory.Internationalization

Namespace roncliProductions.LibWowArmory.Data

    ''' <summary>
    ''' A class that retrieves guild reward information from the Blizzard WoW API.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class GuildRewards
        Inherits WowArmoryData

        Private grRewards As New Schema.GuildRewards.rewards

#Region "WowArmoryData Overrides"

#Region "Public Properties"

        ''' <summary>
        ''' The length of time the data should be cached for, defaulting to 30 days for guild reward information.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides Property CacheLength As New TimeSpan(30, 0, 0, 0)

#End Region

#Region "Protected Properties"

        Protected Overrides ReadOnly Property CacheKey As String
            Get
                Return "LibWowArmory.GuildRewards"
            End Get
        End Property

        Protected Overrides ReadOnly Property URI As Uri
            Get
                Return New Uri("/api/wow/data/guild/rewards", UriKind.Relative)
            End Get
        End Property

#End Region

#End Region

#Region "Properties"

        Private colRewards As Collection(Of Reward)
        ''' <summary>
        ''' A list of guild rewards as returned from the Blizzard WoW API.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Rewards As Collection(Of Reward)
            Get
                Return colRewards
            End Get
        End Property

#End Region

#Region "Methods"

#Region "Public"

        ''' <summary>
        ''' A default constructor to retrieve guild rewards information from the Blizzard WoW API.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            Load()
        End Sub

        ''' <summary>
        ''' A constructor to retrieve character class information for a specific region and language from the Blizzard WoW API.
        ''' </summary>
        ''' <param name="rRegion"></param>
        ''' <param name="lLanguage"></param>
        ''' <remarks></remarks>
        Public Sub New(rRegion As Region, lLanguage As Language)
            Region = rRegion
            Language = lLanguage
            Load()
        End Sub

        ''' <summary>
        ''' Loads the guild rewards.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Load()
            MyBase.Retrieve()
            grRewards = CType(JsonConvert.DeserializeObject(Data, GetType(Schema.GuildRewards.rewards)), Schema.GuildRewards.rewards)
            colRewards = (From r In grRewards.rewards
                          Select New Reward(
                              r.minGuildLevel,
                              CType(r.minGuildRepLevel, Standing),
                              GetRaces(r.races),
                              If(r.achievement Is Nothing, Nothing,
                                 New Achievement(
                                     r.achievement.id,
                                     r.achievement.title,
                                     r.achievement.points,
                                     r.achievement.description,
                                     r.achievement.reward,
                                     New Item(
                                         r.achievement.rewardItem.id,
                                         r.achievement.rewardItem.name,
                                         r.achievement.rewardItem.icon,
                                         CType(r.achievement.rewardItem.quality, Quality)
                                         )
                                     )
                                 ),
                             New Item(
                                 r.item.id,
                                 r.item.name,
                                 r.item.icon,
                                 CType(r.item.quality, Quality)
                                 )
                             )
                         ).ToCollection()
        End Sub

#End Region

#Region "Private"

        Private Shared Function GetRaces(intRaces As Collection(Of Integer)) As Collection(Of Race)
            If intRaces Is Nothing Then Return Nothing
            Dim crRaces As New CharacterRaces()
            Return (From r In crRaces.Races Where intRaces.Contains(r.ID)).ToCollection()
        End Function

#End Region

#End Region

    End Class

End Namespace
